package tcc.wa.service;

import java.util.List;

import tcc.wa.model.Alert;
import tcc.wa.model.AlertType;
import tcc.wa.persistence.dao.AlertDAO;
import android.content.Context;
import android.util.Log;

/**
 * Service class for Alerts.
 */
public class AlertService {

	private final String TAG = "AlertService";
	
	private final AlertDAO alertDAO;
	
	public AlertService(Context context) {
		alertDAO = new AlertDAO(context);
	}
	
	/**
	 * Finds all saved alerts.
	 *
	 * @return the list
	 */
	public List<Alert> findAll() {
		Log.i(TAG, "Obtaining all alerts...");
		List<Alert> alerts = alertDAO.findAll(); 
		Log.i(TAG, "Successfully obtained alerts. Count: " + alerts.size());
		return alerts;
	}
	
	/**
	 * Find alert by type.
	 *
	 * @param type the desired type
	 * @return the list
	 */
	public List<Alert> findByType(AlertType type) {
		return alertDAO.findByType(type);
	}
	
	/**
	 * Saves an alert.
	 *
	 * @param type the alert type
	 * @param value the value of the alert
	 * @param hoursInAdvance the number of hours in advance
	 */
	public void save(AlertType type, double value, int hoursInAdvance) {
		Log.i(TAG, "Saving alert...");
		Alert alert = new Alert();
		alert.setType(type);
		alert.setValue(value);
		alert.setHoursInAdvance(hoursInAdvance);
		alertDAO.save(alert);
		Log.i(TAG, "Successfully saved alert.");
	}
	
	/**
	 * Deletes an alert.
	 *
	 * @param alertId the alert id
	 */
	public void delete(long alertId) {
		Log.i(TAG, "Deleting alert...");
		alertDAO.delete(alertId);
		Log.i(TAG, "Successfully deleted alert.");
	}
}
